package com.amin.concurrent;

public class LockDemo {
	/**
	 * 并发安全 1:加synchronized关键字，效率低，公平锁
	 * 
	 * @author Administrator
	 * 
	 */
	public static String name = "李雷";
	public static String gender = "男";

	public static void main(String[] args) {
		new Thread(new SynRun1()).start();
		new Thread(new SynRun2()).start();
	}
}

class SynRun1 implements Runnable {
	public void run() {
		while (true) {
			synchronized (LockDemo.class) {
				if ("李雷".equals(LockDemo.name)) {
					LockDemo.name = "韩梅梅";
					LockDemo.gender = "女";
				} else {
					LockDemo.name = "李雷";
					LockDemo.gender = "男";
				}
			}
		}
	}
}

class SynRun2 implements Runnable {
	@Override
	public void run() {
		while (true) {
			synchronized (LockDemo.class) {
				System.out.println(LockDemo.name + "," + LockDemo.gender);
			}
		}

	}
}
